package com.yc.myspringboot.ctr;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.yc.myspringboot.listener.demo1.EventDemoPublish;
import com.yc.myspringboot.pojo.Person;
import com.yc.myspringboot.service.TestCacheService;
import com.yc.myspringboot.service.d1.TestService;
import com.yc.myspringboot.service.d2.TestService2;
import com.yc.myspringboot.service.d3.TestService3;
import com.yc.myspringboot.service.demo.TestLock;
import com.yc.myspringboot.service.file.CompressUtil;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
//import com.yc.myspringboot.util.RedisUtil;
//import com.zyxk.sl.fileservice.util.CompressUtil;

@RestController
public class HelloworldController {
	@Autowired
	TestService testService;
	
	@Autowired
	TestService2 testService2;
	
	@Autowired
	TestService3 testService3;
	
//	@Autowired
//	private TestCompan testCompan;
	
	@Autowired
	private EventDemoPublish eventDemoPublish;
	
//	@Autowired
//	private RedisUtil redisUtil;
	
	@Autowired
	private TestCacheService testCacheService;
	
	@Autowired
	private TestLock testLock;
	
	
	/**
	 * 由于spring是单利模式
	 * controller里尽量不要用这种变量，除非业务需要。比如统计接口访问量
	 */
	private Integer cc = 0;

	@RequestMapping("/")
	public String say() {
		cc++;
		return cc+"";
	}
	
	@RequestMapping("/testaop1")
	public void test1() {
		testService.m();
	}
	
	@RequestMapping("/testaop2")
	public void test2() {
		testService2.m();
	}
	
	@RequestMapping("/testaop3")
	public void test3() {
		testService3.m();
	}
	
//	@RequestMapping("/testaop4")
//	public void test4() {
//		testCompan.m1();
//	}
	
	@RequestMapping("/testCompress")
	public String testCompress(String destDir, String sourceFile) {
		System.out.println("-----destDir=" + destDir + ", sourceFile=" + sourceFile);
		try {
			CompressUtil.deCompress(sourceFile, destDir);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "over";
	}
	
	@RequestMapping("/testEvent")
	public void testEvent() {
		eventDemoPublish.publish("-----号外号外-----");
	}
	
//	/**
//	 * 测试redis作为缓存
//	 * @return
//	 */
//	@RequestMapping("/testRedis")
//	@ResponseBody
//	public String testRedisCache() {
//		Person person = (Person) redisUtil.get("mk");
//		if (null == person) {
//			redisUtil.set("mk", new Person("张三" + System.currentTimeMillis(), 23), 10);
//			person = (Person) redisUtil.get("mk");
//		}
//		return "name: " + person.getName() + ", age: " + person.getAge();
//
//	}
//
	/**
	 * spring cache 整合redis作缓存
	 * @return
	 */
	@RequestMapping("/testSpringRedis")
	@ResponseBody
	public String testStringRedisCache() {
//		return testCacheService.m1("aa");
//		return testCacheService.m2("zhangsan", "abcde");
//		return testCacheService.m3("abcde", new Person("zhangsan", 12));
//		return testCacheService.m4("aa");
		return testCacheService.m5("abcde", new Person("zhangsan", 12));
	}
	
	@RequestMapping("/testSpringRedis2")
	@ResponseBody
	public String testStringRedisCache2() {
		return testCacheService.m5("abcde", new Person("zhangsan", 13));
	}
	
	/**
	 * 测试清除缓存
	 * @return
	 */
	@RequestMapping("/testDeleCache")
	@ResponseBody
	public String testDeleCache() {
		return testCacheService.m6("ccccccc2", new Person("zhangsan", 13));
	}
	
	/**
	 * 测试redis分布式锁
	 */
	@RequestMapping("/testLock")
	@ResponseBody
	public Integer testLock() {
		return testLock.m();
	}


	@RequestMapping("/testParam")
	@ResponseBody
	public String testParam(HttpServletRequest request) {
		try {
			String res = getRequestPostStr(request);
			System.out.println(res);
		} catch (IOException e) {
			e.printStackTrace();
		}

		return "success";
	}

	public static String getRequestPostStr(HttpServletRequest request)	throws IOException {
		byte buffer[] = getRequestPostBytes(request);
		String charEncoding = request.getCharacterEncoding();
		if (charEncoding == null){
			charEncoding = "UTF-8";
		}
		return new String(buffer, charEncoding);
	}

	public static byte[] getRequestPostBytes(HttpServletRequest request)
			throws IOException{
		int contentLength=request.getContentLength();
		if(contentLength<0){
			return null;
		}
		byte buffer[]=new byte[contentLength];
		for(int i=0;i<contentLength;){

			int readlen=request.getInputStream().read(buffer,i,contentLength-i);
			if(readlen==-1){
				break;
			}
			i+=readlen;
		}
		return buffer;
	}



}
